//
//  PickerView.m
//  微他
//
//  Created by 微他 on 14-7-31.
//  Copyright (c) 2014年 微他. All rights reserved.
//

#import "PickerView.h"

@interface PickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    NSArray *_arr1;
//    UILabel *_label;
    NSString *_str1;
    UIPickerView *_pickerView;
}
@end

@implementation PickerView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        // Initialization code
    }
    return self;
}

- (void)initInterface
{
    self.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0.5];
    _pickerView = [[UIPickerView alloc] init];
    _pickerView.frame = CGRectMake(10, CGRectGetHeight(self.bounds) - CGRectGetHeight(_pickerView.bounds) - 80, CGRectGetWidth(self.bounds) - 20, CGRectGetHeight(_pickerView.bounds) + 40);
    _pickerView.backgroundColor = [UIColor whiteColor];
//    pickerView.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.clipsToBounds = YES;
    _pickerView.layer.cornerRadius = 6;
    [self addSubview:_pickerView];
//    UIView *view = [[UIView alloc] initWithFrame:];
//    [view addSubview:_pickerView];
//    view.layer.cornerRadius = 6;
//    view.clipsToBounds = YES;
//    _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 40)];
//    _label.textColor = [SetFontColorProperty setCostomColor:CustomButtonGreenColor];
//    _label.textAlignment = NSTextAlignmentCenter;
//    [view addSubview:_label];
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    [button setTitle:NSLocalizedString(@"完成",nil) forState:UIControlStateNormal];
//    button.frame = CGRectMake(CGRectGetWidth(self.bounds) - 60, 0, 60, 40);
//    [button setTitleColor:[SetFontColorProperty setCostomColor:CustomButtonGreenColor] forState:UIControlStateNormal];
//    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
    
//    [view addSubview:button];
//    view.backgroundColor = [UIColor clearColor];
//    [self addSubview:view];
    if (_arr1.count == 2) {
        [self pickerView:_pickerView didSelectRow:0 inComponent:0];
    }else
    {
        [_pickerView selectRow:[_arr1 count] * 50 inComponent:0 animated:YES];
        [self pickerView:_pickerView didSelectRow:[_arr1 count] * 50 inComponent:0];
    }
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_pickerView.frame) + 20, CGRectGetWidth(_pickerView.frame), 45)];
    view.backgroundColor = [UIColor whiteColor];
    view.layer.cornerRadius = 6;
    [self addSubview:view];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button1.frame = CGRectMake(0, 0, CGRectGetWidth(view.bounds) / 2, CGRectGetHeight(view.bounds));
    [button1 setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(quxiao) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button2.frame = CGRectMake(CGRectGetWidth(view.bounds) / 2, 0, CGRectGetWidth(view.bounds) / 2, CGRectGetHeight(view.bounds));
    [button2 setTitle:NSLocalizedString(@"确认", nil) forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor colorWithRed:144.0/255.0 green:87.0/255.0 blue:192.0/255.0 alpha:1] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button2];
    
    UIView *xian = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(view.bounds) / 2, 0, 0.5, CGRectGetHeight(view.bounds))];
    xian.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:xian];
}


//年龄范围
- (void)ageArea
{
    NSMutableArray *arr = [NSMutableArray array];
    for (int i=12; i<=100; i++) {
        [arr addObject:[NSString stringWithFormat:@"%d岁",i]];
    }
    _arr1 = [[NSArray alloc] initWithArray:arr];
    [self initInterface];
}

//身高范围
- (void)heightArea
{
    NSMutableArray *arr = [NSMutableArray array];
    for (int i=120; i<=220; i++) {
        [arr addObject:[NSString stringWithFormat:@"%dcm",i]];
    }
    _arr1 = [[NSArray alloc] initWithArray:arr];
    [self initInterface];
}

//体重范围
- (void)weightArea
{
    NSMutableArray *arr = [NSMutableArray array];
    for (int i=40; i<=100; i++) {
        [arr addObject:[NSString stringWithFormat:@"%dkg",i]];
    }
    _arr1 = [[NSArray alloc] initWithArray:arr];
    [self initInterface];
}

- (void)defaultSelect:(NSString *)str
{
    for (int i=0; i<1000; i++) {
        if ([_arr1[i] integerValue] == [str integerValue]) {
            [_pickerView selectRow:[_arr1 count] * 50 + i inComponent:0 animated:NO];
            [self pickerView:_pickerView didSelectRow:[_arr1 count] * 50 + i inComponent:0];
            break;
        }
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (_arr1.count == 2) {
        return 2;
    }
    return 10000;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        return _arr1[row % [_arr1 count]];
    }
    return nil;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    UILabel *label = (UILabel *)[pickerView viewForRow:row forComponent:component];
    label.textColor = [UIColor colorWithRed:144.0/255.0 green:87.0/255.0 blue:192.0/255.0 alpha:1];
    if (component == 0) {
        if (_arr1[row % [_arr1 count]] != nil) {
            _str1 = _arr1[row % [_arr1 count]];
        }
    }
//    _label.text = label.text;
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] init];
    label.text = _arr1[row % [_arr1 count]];
    label.font = [UIFont boldSystemFontOfSize:17];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [SetFontColorProperty setFontColor:34 float2:34 float3:34];
    return label;
}

- (void)cancel
{
    [_delegete pickerViewDelegatestr1:_str1];
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

- (void)quxiao
{
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
